package com.yt.product.service;

import com.yt.product.models.vo.ProductDetailInfoVO;
import com.yt.product.models.vo.ProductInfoVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.yt.product.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.product.mapper.ProductInfoMapper;
import com.yt.product.models.service.IProductInfoService;
import com.yt.product.models.bo.ProductInfoSaveBO;
import com.yt.product.models.dto.ProductInfoDTO;

import java.util.Arrays;
import java.util.Collections;
import java.util.Optional;

/**
 * 产品类目关系表控制器
 *
 * @author lvzeming
 * @Date 2025-08-11 22:57:57
 */
@DubboService
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfoDTO> implements IProductInfoService{

    @Resource
    private YmlConfig ymlConfig;

    @Override
    public boolean add(ProductInfoSaveBO saveBO) {
        ProductInfoDTO dto = new ProductInfoDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public ProductDetailInfoVO getProductInfoDetail(Long id){
        ProductInfoVO productInfoVO = baseMapper.getProductInfoDetail(id);
        if(null != productInfoVO){
            ProductDetailInfoVO productDetailInfoVO = new ProductDetailInfoVO();
            BeanUtils.copyProperties(productInfoVO, productDetailInfoVO);

            //将字符串通过逗号分割，转换为list对象
            productDetailInfoVO.setPicList(Optional.ofNullable(productInfoVO.getPics())
                    .map(s -> Arrays.asList(s.split(",")))
                    .orElse(Collections.emptyList()));

            return productDetailInfoVO;
        }
        return null;
    }
}
